<?php
	include_once("./global.php");
	session_start();
	if($_SESSION[id]){
		
		if(!empty($_POST['delete_search'])){
			$count = count($_POST['search_kid']);
			$i=0;
			for($i=0;$i<$count;$i++){
				$search_kid = $_POST['search_kid'][$i];
				$sql_search_delete = "update keywordinfo set K_search = NULL where K_KID = '$search_kid'";
				$query_search_delete = $mysqli->query($sql_search_delete);
				if($query_search_delete){
					echo "<script>alert('删除搜索关键词成功')</script>";
				}else{
					echo "<script>alert('删除搜索关键词失败')</script>";
				}
				
			}
		}		
		if(isset($_POST['insert_search'])){
			if($_POST['search_name']){
				$kname = $_POST['search_name'];
				$kposition = $_POST['search_postion'];
				$sql_check = "select K_KID,K_search from keywordinfo where K_Name = '$kname'";
				$query_check = $mysqli->query($sql_check);
				$result_check = $query_check->fetch_assoc();
				if($result_check){
					if($result_check['K_search']){
						if($result_check['K_search'] != '1'){
							echo "<script>alert('你输入的词已经是热门搜索词了')</script>";
						}else{
							echo "<script>alert('你输入的词已经是搜索框内的关键词了')</script>";
						}
					}else{
						if($kposition == '0'){
							$sql_update_search = "update keywordinfo set K_search = '1' where K_Name='$kname'";
							$query_update_search = $mysqli->query($sql_update_search);
							if($query_update_search){
								echo "<script>alert('修改搜索框内关键词成功')</script>";
							}else{
								echo "<script>alert('修改搜索框内关键词失败')</script>";
								echo $mysqli->error;
							}
						}else{
							$count_check = "select K_search from keywordinfo where K_search != ''";
							$count_query_check = $mysqli->query($count_check);
							$count_result_check = $count_query_check->num_rows;
							$kposition = $count_result_check+1;
							$sql_update_search = "update keywordinfo set K_search = '$kposition' where K_Name='$kname'";
							$query_update_search = $mysqli->query($sql_update_search);
							if($query_update_search){
								echo "<script>alert('修改热门关键词成功')</script>";
							}else{
								echo "<script>alert('修改热门关键词失败')</script>";
								echo $mysqli->error;
							}
						}
						
					}
				}else{
					if($kposition == '0'){
						$sql_insert = "insert into keywordinfo (K_KID,K_ID,K_Name,K_search,K_tags) values (NULL,NULL,'$kname','1','0')";
					}else{
						$count_check = "select K_search from keywordinfo where K_search != ''";
						$count_query_check = $mysqli->query($count_check);
						$count_result_check = $count_query_check->num_rows;
						$kposition = $count_result_check+1;
						$sql_insert = "insert into keywordinfo (K_KID,K_ID,K_Name,K_search,K_tags) values (NULL,NULL,'$kname','$kposition','0')";
					}
					$query_insert = $mysqli->query($sql_insert);
					if($query_insert){
						echo "<script>alert('添加搜索关键词成功')</script>";
					}else{
						echo "<script>alert('添加搜索关键词失败')</script>";
					}
					echo "<meta http-equiv=\"refresh\" content=\"1; url='admin_search_mangerment.php' />";
				}
			}else{
				echo "<script>alert('请输入关键词名称')</script>";
				echo "<meta http-equiv=\"refresh\" content=\"1; url='admin_search_mangerment.php' />";
			}
		}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>后台首页</title>
<style>
	body{ background:#e7e7e7; margin:0; padding:0;}
	#container{ width:980px; height:auto; position:relative; margin:0 auto;}
	#container #header{ width:980px; height:90px; position:relative; background:#000; line-height:90px;color:#fff; font-size:24px; text-align:center;}
	.left{width:250px; height:auto; position:relative; float:left;}
	.left table{width:248px; height:auto; position:relative; float:left; background:#fff;}
	.left table td{ height:20px; text-align:center;}
	.left table .selected{ background:#73abe3;}
	#content{ width:730px; height:100%; position:relative; float:left; margin:2px 0px;}
	#content .title{ width:720px; height:25px; position:relative; float:left; line-height:25px; font-size:18px; color:#fff; background:#000;}
	form{margin:20px auto; width:720px; height:auto; position:relative; float:left;}
	form .inpuarea{ width:360px; height:150px;}
</style>
</head>
<body>
	<div id="container">
		<div id="header">
			818苹果站后台管理
		</div>
		<div class="left">
			<table>
				<tr><td style="background:#000000; color:#fff;">站点管理</td></tr>
				<tr><td><a href="admin_site_mangerment.php">全局管理</a></td></tr>
				<tr><td>静态页面管理</td></tr>
				<tr><td><a href="admin_link_mangerment.php?page=1">友情链接管理</a></td></tr>
				<tr><td><a href="admin_member_mangerment.php?page=1">会员管理</a></td></tr>
				<tr><td><a href="admin_tags_mangerment.php?page=1">标签管理</a></td></tr>
				<tr><td class="selected"><a href="admin_search_mangerment.php">搜索管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">文章管理</td></tr>
				<tr><td><a href="admin_category_mangerment.php">文章分类管理</a></td></tr>
				<tr><td><a href="admin_news_add.php">发布文章</a></td></tr>
				<tr><td><a href="admin_news_mangerment.php?page=1">文章管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">应用管理</td></tr>
				<tr><td><a href="admin_appcate_mangerment.php">应用分类管理</a></td></tr>
				<tr><td><a href="admin_app_add.php">添加应用</a></td></tr>
				<tr><td><a href="admin_app_mangerment.php?page=1">应用管理</a></td></tr>
				<tr><td><a href="admin_app_collection.php">应用采集</a></td></tr>
				<tr><td><a href="admin_app_collection_single.php">单应用采集</a></td></tr>
			</table>
		</div>
	<div id="content">
		<div class="title">搜索管理</div>
		<form action="" method="post">
		<table>
			<tr>
				<td><label>添加搜索关键词</label></td>
			</tr>
			<tr>	
				<td><input class="input" type="text" name="search_name" size="50" /></td>
			</tr>
			<tr>
            <td><label>选择位置</label></td>
            </tr>
            <tr>
				<td>
                <input type="radio" name="search_postion" value="0"/>搜索框内关键词<input type="radio" name="search_postion" value="1" />热门搜索关键词
                <!--<input type="radio" name="search_postion" value="1" />搜索框内关键词<input type="radio" name="search_position" value="0" />热门搜索关键词--></td>
			</tr>
			<tr>
				<td><input type="submit" name="insert_search" value="提交" /></td>
			</tr>
		</table>
		</form>
        <form name="form1" action="" method="post">
        <table>
        	<tr>热门搜索关键词</tr>
            <tr><td>关键词编号</td><td>关键词名称</td><td>关键词所在位置</td><td>操作</td></tr>
			<?php
				$sql_search = "select * from keywordinfo where K_search != ''";
				$query_search = $mysqli->query($sql_search);
				while($result_search = $query_search->fetch_assoc()){
			?>
            <tr>
				<td><input name="search_kid[]" type="checkbox" value="<?php echo $result_search['K_KID']?>"/><?php echo $result_search['K_KID'];?></td>
				<td><?php echo $result_search['K_Name']?></td>
				<td><?php echo $result_search['K_search']?></td>
				<td><a href="admin_search_update.php?id=<?php echo $result_search['K_KID'];?>">修改</a></td>
			</tr>  
			<?php
				}
			?>
			<tr><td><input type="checkbox" name="quan" id="quan"  onclick="select_all()" />全选
			<input type="submit" name="delete_search" value="删除" onclick="del()" /> </td></tr>
        </table>
        </form>
	</div>
</body>
<script language="javascript">
	function select_all(){
		var length = document.form1.elements.length;
		var i = 0;
		for(i=0;i<length;i++){
			var checked = document.form1.elements[i];
			if(checked.name != 'quan'){
				if(document.getElementById("quan").checked != false){
					checked.checked = true;
				}else{
					checked.checked = false;
				}
			}
		}
	}
	function del(){
		var length = document.form1.elements.length;
		var i = 0;
		for(i=0;i<length;i++){
			var checked = document.form1.elements[i];
			if(checked.name != 'quan'){
				if(checked.checked == true){
					alert(checked.name+checked.value);
				}
			}
		}
	}
	$(document).ready(function(){
		$("table").find("td").click(function(){
			src = $(this).attr("data-option-value");
			if(!$(this).hasClass("selected")){
				$("table").find("td").removeClass("selected");
				$(this).addClass("selected");
				$("iframe").attr("src",src);
			}			
		});
	});
</script>
</html>
<?php
	}else{
		echo "<script>alert('login')</script>";
		echo "<meta http-equiv=\"refresh\" content=\"1; url='admin_login.php' />";
	}
?>